const Router=require('koa-router')
let router=new Router()
const http=require('https')
const uuid=require('uuid')
let request=require('../libs/request')
const fs=require('fs')
const Path=require('path')
const {root}=require('../libs/config')



// 一键注册登录
router.post('/api/login',async ctx=>{
  let {user,pass}=ctx.request.fields
  let data=await ctx.db.query("select * from user where username=?",[user])
  if(data.length==0){
    let token=uuid.v4()
    let regTime=new Date()
    await ctx.db.query("insert into user(username,password,token,birthday,nickname,faceImg,regTime,sex) values(?,?,?,?,?,?,?,?)",
    [user,pass,token,"1900-01-01","","",regTime,""])
    let data=await ctx.db.query('select * from user where username=?',[user])
    ctx.body={
      err:0,
      msg:"登录成功",
      data
    }
  }else{
    if(data[0].password==pass){
      let token=uuid.v4()
      await ctx.db.query('update user set token=? where username=?',[token,user])
      let data=await ctx.db.query('select * from user where username=?',[user])

      ctx.body={
        err:0,
        msg:"登录成功",
        data
      }
    }else{
      ctx.body={err:0,msg:"密码错误"}
    }
  }
})




// 微信小程序登录
router.post('/api/weixinLogin',async ctx=>{
  // 参数 code userInfo
  let {code,nickname,faceImg}=ctx.request.fields;
  // console.log(code,nickname,faceImg)
  let {wx}=require('../libs/config');
  let {AppId,AppSecret}=wx;
  let body = await request(`https://api.weixin.qq.com/sns/jscode2session?appid=${AppId}&secret=${AppSecret}&js_code=${code}&grant_type=authorization_code`)
  let {openid}=JSON.parse(body);
      // console.log(body.openid)
      // console.log(openid)
  let data=await ctx.db.query("select * from user where username=?",[openid])
  console.log(data)
      // 判断没有此账号注册
  if(data.length==0){
    let token=uuid.v4()
    let regTime=new Date()
    await ctx.db.query("insert into user(username,password,token,birthday,nickname,faceImg,regTime,sex) values(?,?,?,?,?,?,?,?)",
    [openid,"",token,"1900-01-01",nickname,faceImg,regTime,""])
    let user=await ctx.db.query('select * from user where username=?',[openid])
    // console.log(user)
    ctx.body={
      err:0,
      msg:"登录成功",
      data:user
    }
  }else{
    let token=uuid.v4()
    await ctx.db.query('update user set token=?,faceImg=? where username=?',[token,faceImg,openid])
    let data=await ctx.db.query('select * from user where username=?',[openid])
    ctx.body={
      err:0,
      msg:"登录成功",
      data
    }
  }



})



// app第三方登录
router.post('/api/appLogin',async ctx=>{
  // 参数 code userInfo
  let {openid,nickname,faceImg}=ctx.request.fields;
      // console.log(body.openid)
      // console.log(openid)
  let data=await ctx.db.query("select * from user where username=?",[openid])
  console.log(data)
      // 判断没有此账号注册
  if(data.length==0){
    let token=uuid.v4()
    let regTime=new Date()
    await ctx.db.query("insert into user(username,password,token,birthday,nickname,faceImg,regTime,sex) values(?,?,?,?,?,?,?,?)",
    [openid,"",token,"1900-01-01",nickname,faceImg,regTime,""])
    let user=await ctx.db.query('select * from user where username=?',[openid])
    // console.log(user)
    ctx.body={
      err:0,
      msg:"登录成功",
      data:user
    }
  }else{
    let token=uuid.v4()
    await ctx.db.query('update user set token=?,faceImg=? where username=?',[token,faceImg,openid])
    let data=await ctx.db.query('select * from user where username=?',[openid])
    ctx.body={
      err:0,
      msg:"登录成功",
      data
    }
  }
})

router.post('/api/uploadFace',async ctx=>{
  let token=ctx.header.token
  let path=ctx.request.fields.img[0].path
  let str=path.split('upload_')[1]
  path=root+'/upload/upload_'+str
  console.log(path)
  let user=await ctx.db.query('select * from user where token=?',[token])
  if(user.length==0){
    await ctx.unlink(path)
    ctx.body={err:1,msg:"账号已在异地登录"}
  }else{
    let unlinkUrl=Path.join(__dirname,'../static'+user[0].faceImg.split(root)[1])
    console.log(unlinkUrl)
    await ctx.unlink(unlinkUrl)
    await ctx.db.query('update user set faceImg=? where token=?',[path,token])
    let data=await ctx.db.query('select * from user where token=?',[token])
    ctx.body={
      err:0,
      data
    }
  }

})
module.exports=router